/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * <p>Project: app2022 - Ex2
 * <p>Powered by webrx On 2022-01-17 16:48:58
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex2 {
    public static long rows = 0;
    public static long filecount = 0;

    public static void main(String[] args) {
        //6828行，159java源码文件
        //count(new File("c:/app2022"));

        //java 17.0.1
        //4683767行
        //14173文件

        //java 1.8 64
        //2403421行
        //7729文件
        //count(new File(System.getProperty("user.dir")));

        count(new File("D:\\ftp\\logs\\代码项目code"));

        System.out.println(rows);
        System.out.println(filecount);
    }

    /**
     * Description: 递归统计java文件总数
     *
     * @date: 2022/1/17 17:07
     * @params: [java.io.File]
     * @return: void
     */
    public static void count(File file) {
        for (File t : file.listFiles()) {
            if (t.isDirectory()) {
                count(t);
            } else if (t.isFile() && t.getName().toLowerCase().endsWith(".java")) {
                rows += rows(t);
                System.out.printf("%s(%d)[%3$tF %<tT]%n", t.getAbsolutePath(), rows(t), t.lastModified());
                ++filecount;
            }
        }
    }

    /**
     * Description: 返回java源码文件的行数
     *
     * @date: 2022/1/17 16:51
     * @params: [java.io.File]
     * @return: long
     */
    public static long rows(File file) {
        long rows = 0;
        try {
            var br = new BufferedReader(new FileReader(file));
            rows = br.lines().count();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }
}
